using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class LampItem : BaseItem
	{
		public enum State
		{
			Close = 0,
			Open = 1
		}

		public State state;

		private bool hadEletric = true;

		private Transform lightTrans;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			lightTrans = model.Find("LightFront");
			if (lightTrans == null)
			{
				lightTrans = model.Find("LightBack");
				lightTrans.SetLocalZ(1E-05f);
			}
			else
			{
				lightTrans.SetLocalZ(-1E-05f);
			}
			SetState((State)data.state);
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (state == State.Close)
			{
				SetState(State.Open);
			}
			else
			{
				SetState(State.Close);
			}
			Singleton<AudioManager>.Instance.PlaySound("sound_lamp_open_close");
		}

		public void SetState(State val)
		{
			state = val;
			if (!(lightTrans == null) && hadEletric)
			{
				if (state == State.Close)
				{
					lightTrans.gameObject.SetActive(false);
					GlobalEventSystem.Fire(new BaseEvent("game.open_close_lamp", new object[2] { archiveData.index, false }));
				}
				else
				{
					lightTrans.gameObject.SetActive(true);
					GlobalEventSystem.Fire(new BaseEvent("game.open_close_lamp", new object[3]
					{
						archiveData.index,
						true,
						float.Parse(base.itemData.scriptParams)
					}));
				}
			}
		}

		public void HadEletric(bool val)
		{
			if (hadEletric == val)
			{
				return;
			}
			hadEletric = val;
			if (state == State.Open)
			{
				if (hadEletric)
				{
					lightTrans.gameObject.SetActive(true);
					GlobalEventSystem.Fire(new BaseEvent("game.open_close_lamp", new object[3]
					{
						archiveData.index,
						true,
						float.Parse(base.itemData.scriptParams)
					}));
				}
				else
				{
					lightTrans.gameObject.SetActive(false);
					GlobalEventSystem.Fire(new BaseEvent("game.open_close_lamp", new object[2] { archiveData.index, false }));
				}
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = (int)state;
		}
	}
}
